class StoriesController < ApplicationController
  # GET /stories
  # GET /stories.xml
  
  STORY_STATUSES = [[ "Open", "open" ], ["Closed", "closed"]]
  
  def index
    @stories = Story.find(:all, :include => { :sprint, :user })
 
    #list
    #render :action => 'list'
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @stories }
    end
  end

  def list
    $story_pages, @stories = paginate(:stories, :per_page => 3)
  end
  
  # GET /stories/1
  # GET /stories/1.xml
  def show
    @story = Story.find(params[:id], :include => { :sprint, :user })

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @story }
    end
  end

  # GET /stories/new
  # GET /stories/new.xml
  def new
    @story = Story.new
    @story_statuses = STORY_STATUSES
    @all_sprints = Sprint.find(:all, :order=>"title").collect { |p| [p.title, p.id] }
    @all_users = User.find(:all, :order=>"login").collect { |p| [p.login, p.id] }
         
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @story }
    end
  end

  # GET /stories/1/edit
  def edit
    @story = Story.find(params[:id])
    @story_statuses = STORY_STATUSES
    @all_sprints = Sprint.find(:all, :order=>"title").collect { |p| [p.title, p.id] }
    @all_users = User.find(:all, :order=>"login").collect { |p| [p.login, p.id] }
  end

  # POST /stories
  # POST /stories.xml
  def create
    @story = Story.new(params[:story])
    @story_statuses = STORY_STATUSES
    @all_sprints = Sprint.find(:all, :order=>"title").collect { |p| [p.title, p.id] }
    @all_users = User.find(:all, :order=>"login").collect { |p| [p.login, p.id] }

    respond_to do |format|
      if @story.save
        flash[:notice] = 'Story was successfully created.'
        format.html { redirect_to(@story) }
        format.xml  { render :xml => @story, :status => :created, :location => @story }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @story.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stories/1
  # PUT /stories/1.xml
  def update
    @story = Story.find(params[:id])
    @story_statuses = STORY_STATUSES
    @all_sprints = Sprint.find(:all, :order=>"title").collect { |p| [p.title, p.id] }
    @all_users = User.find(:all, :order=>"login").collect { |p| [p.login, p.id] }

    respond_to do |format|
      if @story.update_attributes(params[:story])
        flash[:notice] = 'Story was successfully updated.'
        format.html { redirect_to(@story) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @story.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stories/1
  # DELETE /stories/1.xml
  def destroy
    @story = Story.find(params[:id])
    @story.destroy

    respond_to do |format|
      format.html { redirect_to(stories_url) }
      format.xml  { head :ok }
    end
  end
end
